package cn.itsource.transactionmessage;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class TransactionProducer {
    public static void main(String[] args) throws Exception {
        //1.创建事务生产者组
        TransactionMQProducer producer = new TransactionMQProducer("producer-hello");
        //2.设置NameServer地址
        producer.setNamesrvAddr("127.0.0.1:9876");

        //设置监听
        producer.setTransactionListener(new MyTransactionCheckListener());
        //3.启动producer实例
        producer.start();

        //4.创建消息
        Message message = new Message("log-topic1", "info-tag", "这是一个info666信息".getBytes(RemotingHelper.DEFAULT_CHARSET));

        //5.发送事务消息
        TransactionSendResult transactionSendResult = producer.sendMessageInTransaction(message,"快点下雨");

        System.out.println("发送完毕，结果: "+transactionSendResult);
        //6.关闭producer实例
        producer.shutdown();
    }
}
